23 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
24 #define For(i, a, b) for (int i=(a); i<(b); ++i)
25 #define D(x) cout << #x " is " << x << endl
27 int start
[8][8], end
[8][8], cur
[8][8];
29 int di
[] = {-1, +1, +0, +0};
30 int dj
[] = {+0, +0, -1, +1};
32 typedef unsigned long long int board
;
34 #define outside(i, j) ((i) < 0 or (i) >= 8 or (j) < 0 or (j) >= 8)
36 board
make_board(int b
[8][8]){
38 for (int i
= 0; i
< 8; ++i
){
39 for (int j
= 0; j
< 8; ++j
){
41 ans
|= (1ULL << (i
* 8 + j
));
48 void unmake_board(board b
, int out
[8][8]){
49 for (int i
= 0; i
< 8; ++i
){
50 for (int j
= 0; j
< 8; ++j
){
54 for (int i
= 0; i
< 8; ++i
){
55 for (int j
= 0; j
< 8; ++j
){
56 if (b
& (1ULL << (i
* 8 + j
))){
63 void explore(board s
, set
<board
> &seen
) {
70 while (boards
.size() > 0) {
71 board b
= boards
.front(); boards
.pop();
72 char d
= distance
.front(); distance
.pop();
74 if (d
== 4) continue; // Too far
78 //printf("d is %d\n", (int)d);
79 //printf("cur is\n"); for (int i = 0; i < 8; ++i){ for (int j = 0; j < 8; ++j){ printf("%d ", cur[i][j]); } puts(""); } puts("");
81 for (int i
= 0; i
< 8; ++i
){
82 for (int j
= 0; j
< 8; ++j
){
83 if (!cur
[i
][j
]) continue;
84 for (int k
= 0; k
< 4; ++k
){
87 if (outside(ni
, nj
)) continue;
88 if (cur
[ni
][nj
]){ //taken, let's move one extra cell
91 if (outside(ni
, nj
)) continue;
92 if (cur
[ni
][nj
]) continue;
96 swap(cur
[ni
][nj
], cur
[i
][j
]);
98 board cur_mask
= make_board(cur
);
99 if (seen
.count(cur_mask
) == 0){
100 // not seen, let's enqueue this new state
101 seen
.insert(cur_mask
);
102 boards
.push(cur_mask
);
103 distance
.push(d
+ 1);
105 swap(cur
[ni
][nj
], cur
[i
][j
]);
116 for (int i
= 0; i
< 8; ++i
){
117 for (int j
= 0; j
< 8; ++j
){
118 start
[i
][j
] = end
[i
][j
] = 0;
122 for (int i
= 0; i
< 4; ++i
){
124 if (scanf("%d%d", &r
, &c
) != 2) return 0;
126 assert(!start
[r
][c
]);
129 for (int i
= 0; i
< 4; ++i
){
131 if (scanf("%d%d", &r
, &c
) != 2) return 0;
139 s1
.insert(make_board(start
));
140 s2
.insert(make_board(end
));
142 explore(make_board(start
), s1
);
143 explore(make_board(end
), s2
);
145 //printf("s1.size = %d\n", s1.size());
149 if (s2
.count(*x
) > 0){
154 puts(ans
? "YES" : "NO");